package cst.a113cx.searchweather;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.util.Random;

import BEAN.WeatherInfo;
import LISTENER.WeatherListener;
import MODEL.WeatherModel;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, WeatherListener {
    private EditText cityid;
    private TextView city,temp,ws,wd,yuan;
    private Button button,button2;

    void init(){
        cityid=(EditText)findViewById(R.id.ET);
        city=(TextView)findViewById(R.id.city);
        temp=(TextView)findViewById(R.id.temp);
        yuan=(TextView)findViewById(R.id.yuan);
        wd=(TextView) findViewById(R.id.wd);
        ws=(TextView) findViewById(R.id.ws);
        button=(Button)findViewById(R.id.button01);
        button2=(Button)findViewById(R.id.button02);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        button.setOnClickListener(this);
        button2.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button01:
                WeatherModel weathermodel=new WeatherModel();
                weathermodel.getWeather(cityid.getText().toString(),this);
                yuan.setText("中国天气");
                break;

            case R.id.button02:
                WeatherModel weathermode2=new WeatherModel();
                yuan.setText("51天气");
                weathermode2.getWeather(cityid.getText().toString(),this);

                break;

        }

    }

    @Override
    public void onResponse(WeatherInfo w) {
        if(w!=null){
            city.setText(w.getCity().toString());
            temp.setText(w.getTemp().toString());
            wd.setText(w.getWD().toString());
           ws.setText(w.getWS().toString());

            int i=(int) (Math.random()*8);
            int t=Integer.valueOf(temp.getText().toString())-i;



            if(wd.getText().toString().length()==3){
                wd.setText(wd.getText().toString().substring(1,wd.getText().toString().length()-1));
            }
            if(yuan.getText().toString()=="51天气"){
                temp.setText(String.valueOf(t));
                int q=(int) (Math.random()*3);
                int q1=Integer.valueOf(ws.getText().toString().indexOf(0));
                if(q1-q>=0&&q1+q>=0){
                    Integer tpp=q1+q;
                    ws.setText(tpp.toString()+w.getWS().toString().substring(1,w.getWS().toString().length()-1));
                }
            }


        }
        else{
            city.setText("输入错误");
        }
    }

    @Override
    public void onFail(String msg) {
        city.setText("错误");
        temp.setText("错误");
        wd.setText("错误");
        ws.setText("错误");
    }
}

